package com.nizi.wordoperate.controller;

import com.nizi.wordoperate.utils.DownLoadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


@RestController
@RequestMapping(value = "/api")
public class DownloadController {

    @Value("${workDir}")
    private String workDir;
    private static final Logger logger = LoggerFactory.getLogger(DownloadController.class);


    @RequestMapping("/down")
    public void down(HttpServletResponse response,String token) throws Exception {
        String zipFilename = workDir+"tempFile/tempFile_"+token+".zip";
        File file = new File(zipFilename);
        if (!file.exists()) {
            logger.error(zipFilename+" 不存在");
            return ;
        }
         response.reset();
         logger.info("下载合同"+file.getName());
         DownLoadUtil.downloadZip(file, response);
    }
}
